package com.okami.config;

import org.aopalliance.intercept.MethodInvocation;
import org.springframework.security.access.ConfigAttribute;
import org.springframework.security.web.FilterInvocation;
import org.springframework.security.web.access.intercept.FilterInvocationSecurityMetadataSource;
import org.springframework.stereotype.Component;
import org.springframework.security.access.SecurityConfig;
import org.springframework.util.AntPathMatcher;

import java.util.Collection;

/**
 * @description: 不使用方法上的注解控制单个api权限控制，而是通过数据库统一配置请求地址和权限关系
 * @author: TanJingyu
 * @create: 2020-10-12 11:36
 **/
//@Component
public class MetaDataAttributeConfig implements FilterInvocationSecurityMetadataSource {
    AntPathMatcher antPathMatcher = new AntPathMatcher();

    /**
     *  根据请求地址，匹配此地址所需权限/角色
     *  ROLE_开头是角色，其他是操作权限
     */
    @Override
    public Collection<ConfigAttribute> getAttributes(Object object) throws IllegalArgumentException {
        return SecurityConfig.createList("ROLE_admin");
    }

    @Override
    public Collection<ConfigAttribute> getAllConfigAttributes() {

        return null;
    }

    @Override
    public boolean supports(Class<?> clazz) {
        return (MethodInvocation.class.isAssignableFrom(clazz));
    }
}
